// const API_URL = "http://47.96.229.51:8081/hezan/";//V2.0测试 
// const API_URL = "https://taotaocangapi.manybrother.com/";//V2.0测试 
const API_URL = "https://api.taotaocang.com/";//正式

// 
function Get(url, params, header) {
  var header=header||{}
  if (wx.getStorageSync('token') && (new Date().getTime() - wx.getStorageSync('loginTime') < 604800000)){
    header['X-Access-Token']=wx.getStorageSync('token')
  }
  wx.showNavigationBarLoading();
  let promise = new Promise(function (resolve, reject) {
    let Header = { 'Content-Type': 'application/json' }
    if (header) {
      for (let key in header) {
        Header[key] = header[key]
      };
    }

    wx.request({

      url: API_URL + url,

      data: params,

      method: 'GET',

      header: Header,

      success: res => {

        resolve(res.data);

      },

      fail: res => {
        wx.showToast({
          title: '网络错误,请稍后重试',
          icon: 'none',
          duration: 2000
        })
        reject(res.data)

      },

      complete:()=> {

        wx.hideNavigationBarLoading();

      }

    })

  });

  return promise

}

function Post(url, params, header) {
  var header=header||{}
  if (wx.getStorageSync('token') && (new Date().getTime() - wx.getStorageSync('loginTime') < 604800000)){
    header['X-Access-Token']=wx.getStorageSync('token')
  }
  wx.showNavigationBarLoading();
  let promise = new Promise(function (resolve, reject) {
    let Header = { 'Content-Type': 'application/x-www-form-urlencoded' }
    if (header){
      for (let key in header) {
        Header[key] = header[key]
      };
    }

    wx.request({

      url: API_URL + url,

      data: params,

      method: 'POST',

      header: Header,

      success: res => {

        resolve(res.data);

      },

      fail: res => {
        wx.showToast({
          title: '网络错误,请稍后重试',
          icon: 'none',
          duration: 2000
        })
        reject(res.data)

      },

      complete:()=> {

        wx.hideNavigationBarLoading();

      }

    })

  });

  return promise

}

function JsonPost(url, params, header) {
  
  var header=header||{}
  if (wx.getStorageSync('token') && (new Date().getTime() - wx.getStorageSync('loginTime') < 604800000)){
    header['X-Access-Token']=wx.getStorageSync('token')
  }
  wx.showNavigationBarLoading();
  let promise = new Promise(function (resolve, reject) {
    let Header = { 'Content-Type': 'application/json' }
    if (header) {
      for (let key in header) {
        Header[key] = header[key]
      };
    }
    wx.request({

      url: API_URL + url,

      data: JSON.stringify(params),

      method: 'POST',

      header: Header,

      success: res => {
        if (res.data.message==="无效token") {
          getApp().globalData.login=false
          wx.setStorageSync('token', '')
          getApp().ttClogin()
        }
        resolve(res.data);

      },

      fail: res => {
        wx.showToast({
          title: '网络错误,请稍后重试',
          icon: 'none',
          duration: 2000
        })
        reject(res.data);

      },

      complete:()=> {

        wx.hideNavigationBarLoading();

      }

    })

  });

  return promise

}

module.exports = {

  Get,

  Post,

  JsonPost,

  API_URL

}
